package effect;

import board.BoardUtil;
import board.PatternType;
import character.MyCharacter;
import com.jme3.math.Vector3f;
import game.GameUtil;
import java.util.Collection;
import java.util.HashSet;
import misc.Vector3i;

public abstract class AreaEffect extends Effect {
    protected Vector3i target;
    protected PatternType effectPattern;
    protected Integer effectSize;
    protected Collection<Vector3i> affectedLocations = new HashSet<Vector3i>();

    @Deprecated
    protected AreaEffect() {}

    public AreaEffect(MyCharacter owner) {
        super(owner);
    }

    public Vector3i getTarget() {
	return target;
    }
    public void setTarget(Vector3i target) {
	this.target = target;
        repopulateAffectedLocations();
    }
    public PatternType getEffectPattern() {
	return effectPattern;
    }
    public void setEffectPattern(PatternType effectPattern) {
	this.effectPattern = effectPattern;
    }
    public int getEffectSize() {
	return effectSize;
    }
    public void setEffectSize(int effectSize) {
	this.effectSize = effectSize;
    }
    public Vector3f getParticleEffectLocation() {
	return BoardUtil.boardToWorld(target);
    }
    public Collection<Vector3i> getAffectedLocations() {
        if(affectedLocations.isEmpty() && target != null)
            repopulateAffectedLocations();
        return affectedLocations;
    }
    protected void repopulateAffectedLocations() {
        affectedLocations.clear();
        if(effectPattern != null && effectSize != null) {
            if(effectPattern == PatternType.LINE) {
                boolean done = false;
                for(Vector3i occupiedLocation : owner.getOccupiedLocations()) {
                    if(!done) {
                        Collection<Vector3i> locations = BoardUtil.getPatternLocations(effectPattern, effectSize, false, occupiedLocation, target);
                        if(!locations.isEmpty() && !GameUtil.containsAny(locations, owner.getOccupiedLocations())) {
                            affectedLocations = locations;
                            done = true;
                        }
                    }
                }
            } else {
                affectedLocations = BoardUtil.getPatternLocations(effectPattern, effectSize, true, target);
            }
        }
    }
}
